package com.xt.yncu.util;

import gui.ava.html.image.generator.HtmlImageGenerator;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import net.sf.json.JSONObject;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.xt.yncu.util.Config;

public class OfficeOther {
	private static Logger log = Logger.getLogger(OfficeOther.class);
	public static final String UTF_8 = "UTF-8";
	public String ENCODE = "GBK";
	public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
	//private HttpPostUtil postUtil;
	private HtmlImageGenerator htmlToImage;
	// PPT生成缩略图大小
	public int pptMaxPicWidth = 700, pptMaxPicHeight = 700;
	public String savePath;
	/* 是否为word文档 */
	public boolean docFlag = false;
	/* 文件原始名称 */
	public String fileOldName;
	/* 取得文件中文名(无后缀) */
	public String fileSpellName;
	/* 文件原始大小 */
	public long fileOldSize;

	/* 生成图片数量 */
	public int picNum;
	/* 生成Html路径 */
	public String createHtmlPath;
	/* 文件后缀名 */
	public String fileExtension;

	/** 附件下载失败创建文件 */
	public int errorCreateFile(String savePath, Config config) {
		StringBuilder actionPath = new StringBuilder();
		actionPath.append(config.getLanAddress()).append(config.errorAction);
		StringBuilder errorPath = new StringBuilder();
		errorPath.append(createFolder(config.getErrorFolder()));
		errorPath.append("\\").append(getFileName(savePath, 1)).append(".txt");
		/*if (config.remote) {
			postUtil = new HttpPostUtil(actionPath.toString());
			postUtil.send();
			postUtil.clearAllParameters();
		} else {*/
			String content = "File download failed";
			writeFile(content, errorPath.toString());
		//}
		return 6;
	}

	/** 转换的文件后缀 */
	public String transformFileName(int saveType) {
		String returnExit = null;
		switch (saveType) {
		case 17:
			returnExit = "pdf";
			break;
		case 0:
			returnExit="doc";
			break;
		case 1:
			returnExit="xls";
			break;
		default:
			returnExit = "htm";
			break;
		}
		return returnExit;
	}

	/** 创建文件夹 */
	public String createFolder(String folderName) {
		File file = new File(getCurrentPath() + "\\" + folderName);
		if (!file.exists()) {
			file.mkdirs();
		}
		return String.valueOf(file);
	}

	public String createFolerItem(String path, String folder) {
		File file = new File(path + "\\" + folder);
		if (!file.exists()) {
			file.mkdirs();
		}
		return String.valueOf(file);
	}

	/** 取得当前路径 */
	public String getCurrentPath() {
		String currentPath = (String) this.getClass().getClassLoader()
				.getResource("").getPath();
		return (String) currentPath.subSequence(1,
				currentPath.indexOf("WEB-INF"));
	}

	/** 如果图片大于指定尺寸以缩略图的方式保存图片 */
	public void saveSmailPic(File file, String savePath, int maxPicWidth,
			int maxPicHeight) {
		OutputStream os = null;
		InputStream is = null;
		FileInputStream fis = null;
		FileOutputStream fos = null;
		Float imageWidth, imageHeight;
		Integer imageNewWidth = 0, imageNewHeight = 0;
		try {
			fis = new FileInputStream(file);
			Image image = ImageIO.read(fis);
			imageWidth = (float) image.getWidth(null);
			imageHeight = (float) image.getHeight(null);
			if (imageWidth > maxPicWidth || imageHeight > maxPicHeight) {
				if (imageWidth > imageHeight) {
					imageNewWidth = (int) ((imageWidth > maxPicWidth) ? maxPicWidth
							: imageWidth);
					imageNewHeight = (int) ((imageHeight / imageWidth) * imageNewWidth);
				} else if (imageWidth < imageHeight) {
					imageNewHeight = (int) ((imageHeight > maxPicHeight) ? maxPicHeight
							: imageHeight);
					imageNewWidth = (int) ((imageWidth / imageHeight) * imageNewHeight);
				} else {
					imageNewWidth = (int) ((imageWidth > maxPicWidth) ? maxPicWidth
							: imageWidth);
					imageNewHeight = (int) ((imageHeight > maxPicHeight) ? maxPicHeight
							: imageHeight);
				}
				BufferedImage tag = new BufferedImage(imageNewWidth,
						imageNewHeight, BufferedImage.TYPE_INT_RGB);
				tag.getGraphics().drawImage(image, 0, 0, imageNewWidth,
						imageNewHeight, null);
				fos = new FileOutputStream(new File(savePath));
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
				encoder.encode(tag);

			} else {
				System.out.println(savePath);
				is = new FileInputStream(file);
				os = new FileOutputStream(new File(savePath));
				int buffer = 500 * 1024;
				byte[] bytes = new byte[buffer];
				int len = 0;
				while ((len = is.read(bytes, 0, buffer)) > 0) {
					os.write(bytes, 0, len);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();
				if (fos != null)
					fos.close();
				if (os != null)
					os.close();
				if (is != null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 取得转换的文件名
	public String getFileName(String filePath, int getType) {
		File file = new File(filePath);
		// System.out.println(file.getName());
		String fileName = file.getName();
		if (getType == 1) {
			fileName = fileName.substring(0, fileName.indexOf("."));
		}
		// System.out.println(fileName);
		return fileName;
	}

	// 取得中文首字母
	public String cn2FirstSpell(String chinese) {
		StringBuilder pybf = new StringBuilder();
		char[] arr = chinese.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] > 128) {
				try {
					String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i],
							defaultFormat);
					if (_t != null) {
						pybf.append(_t[0].charAt(0));
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pybf.append(arr[i]);
			}
		}
		return pybf.toString().replaceAll("\\W", "").trim().toLowerCase();
	}

	// 文件名转为英文
	public String cn2Spell(String chinese) {
		try {
			String fileName =StringFilter(chinese.replaceAll(" ",""));
			HanyuPinyinOutputFormat spellFormat = new HanyuPinyinOutputFormat();
			spellFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
			spellFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
			spellFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
			return PinyinHelper.toHanyuPinyinString(fileName, spellFormat, "").toLowerCase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	// 生成html
	public String createHtml(String savePath, String filePath, String fileName,
			long fileSize, int picNum, Config config) {
		int h = 30, w = 15;
		StringBuilder html = new StringBuilder();
		/*
		 * html .append(
		 * "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
		 * ); html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); html
		 * .append(
		 * "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title></title><meta name='viewport' content='width=device-width, user-scalable=yes'/>"
		 * );
		 */
		String s = this.s;
		html.append(s);

		// html.append("<script type=\"text/javascript\">");
		html.append("var maxPage=").append(picNum).append(";");
		html.append("var httpHost=\"").append(config.getNetworkAddress())
				.append("\";");
		html.append("var folder =\"").append(config.getPicFolder())
				.append("/\";");
		html.append("var filePath=\"").append(filePath).append("/\";");
		html.append("var fileExit=").append("\".jpg\";");

		html.append("function setImage(page){");
		html.append("var imagePath=httpHost+\"/\"+folder+filePath+page+fileExit;");
		html.append("document.getElementById(\"currentPage\").value=page;");
		html.append("document.getElementById(\"pageNum\").value=(page+1);");
		html.append("document.getElementById(\"pageNum1\").value= (page + 1);");
		html.append("document.getElementById(\"pageNum\").innerHTML = (page + 1);");
		html.append(" document.getElementById(\"pageNum1\").innerHTML = (page + 1);");
		html.append("document.getElementById(\"showImage\").src=imagePath;");
		html.append("loadPic(imagePath);");
		html.append("}");

		html.append("function prePage(){");
		html.append("var currentPage;");
		html.append("currentPage=parseInt(document.getElementById(\"currentPage\").value);");
		html.append("if(currentPage>0){");
		html.append("currentPage-=1;");
		html.append("setImage(currentPage);");
		html.append("}}");

		html.append("function nextPage(){");
		html.append("var currentPage;");
		html.append("currentPage=parseInt(document.getElementById(\"currentPage\").value);");
		html.append("if(currentPage<maxPage-1){");
		html.append("currentPage+=1;");
		html.append("setImage(currentPage);");
		html.append("}}");

		// 页面跳转JS
		html.append("function goPage(){");
		html.append("var pageNum;");
		html.append("pageNum=parseInt(document.getElementById(\"pageNum\").value);");
		html.append("if(isNaN(pageNum)){");
		html.append("pageNum=1;");
		html.append("}");
		html.append("if(pageNum<=0){");
		html.append("pageNum=1;");
		html.append("}");
		html.append("if(pageNum>maxPage){");
		html.append("pageNum=maxPage;");
		html.append("}");
		html.append("document.getElementById(\"pageNum\").value=pageNum;");
		html.append("document.getElementById(\"pageNum1\").value=pageNum;");
		html.append("pageNum-=1;");
		html.append("setImage(pageNum);");
		html.append("}");

		html.append("function changePage(){");
		html.append("var pageNum;");
		html.append("pageNum = parseInt(document.getElementById(\"pageNum1\").value);");
		html.append("document.getElementById(\"pageNum\").value=pageNum;");
		html.append("}");

		html.append("function loadPic(url) {");
		html.append("var img = new Image();");
		html.append("document.getElementById(\"imageHtml\").style.display = \"none\";");
		html.append("document.getElementById(\"loading\").style.display = \"block\";");
		html.append(" img.onload = function() {");
		html.append("document.getElementById(\"loading\").style.display = \"none\";");
		html.append("document.getElementById(\"imageHtml\").style.display = \"block\";");
		html.append("}");
		html.append("\n");
		html.append("img.onerror = function() {");
		html.append("document.getElementById(\"loading\").style.display = \"none\";");
		// html.append("document.getElementById(\"loadError\").style.display = \"block\";");
		html.append("}");
		html.append("\n");
		html.append("img.src = url;");
		html.append("}");

		html.append("window.onload=function(){");
		html.append("loadPic(httpHost+\"/\"+folder+\"/\"+filePath+\"0.jpg\");");
		html.append("}");

		html.append("</script>");
		html.append("</head>");
		html.append("<body><div class='main'>");

		s = "<div class=\"bnt_bar\">"
				+ "<input name=\"hisBtn\" type=\"button\" value=\"返 回\" onclick=\"javascript:history.go(-1);setBnt(this);\" />";
		if (picNum > 1) {
			s += "<input type=\"button\" value=\"上一页\" onclick=\"prePage();setBnt(this);\" />"
					+ "<input type=\"button\" value=\"下一页\" onclick=\"nextPage();setBnt(this);\" />";
		}

		/* 顶部按钮 */
		/*
		 * html.append(s) .append("<div><span id=\"pageNum\">1</span>/<span>")
		 * .append(picNum) .append("页</span></div></div>");
		 */

		html.append("<div id=\"loading\" class=\"load\"><img src=\"")
				.append(config.getNetworkAddress())
				.append("/loading.gif\" /><br/> 文件加载中...</div>");
		html.append("<div id=\"imageHtml\" class=\"showIMG\"><img src=\"");
		html.append(filePath).append("/0.jpg\" id=\"showImage\"/>");
		html.append("<input type=\"hidden\" id=\"fileName\"  value=\"")
				.append(fileName).append("\"/>");
		html.append("<input type=\"hidden\" id=\"currentPage\"  value=\"0\">");
		html.append("<input type=\"hidden\" id=\"fileSize\"  value=\"")
				.append(fileSize).append("\"/>");
		html.append("<input type=\"hidden\" id=\"picNum\"  value=\"")
				.append(picNum).append("\"/>");
		html.append("</div>");
		html.append(s)
				.append("<div style=\"color:red\"><span id=\"pageNum1\">1</span>/<span>")
				.append(picNum)
				.append("页</span><span id=\"pageNum\" style='display:none'>1</span></div></div>");
		html.append("</div></body></html>");

		/*
		 * html .append(
		 * "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"center\"><input name=\"hisBtn\" type=\"button\" value=\"返回\" onclick=\"javascript:history.go(-1);\" style=\"width:"
		 * +(w*3)+"px;height:"+h+"px;font-size:"+w+"px;font-weight:bold;\"/>");
		 * if (picNum > 1) { html .append(
		 * "&nbsp;&nbsp;<input type=\"button\" value=\"上一页\" onclick=\"prePage()\"  style=\"width:"
		 * +(w*4)+"px;height:"+h+"px;font-size:"+w+
		 * "px;font-weight:bold;\"/>&nbsp;&nbsp;<input type=\"button\" onclick=\"nextPage()\" value=\"下一页\"  style=\"width:"
		 * +(w*4)+"px;height:"+h+"px;font-size:"+w+"px;font-weight:bold;\"/>");
		 * // html.append(
		 * "&nbsp;&nbsp;<input type=\"text\" id=\"pageNum\"  value=\"1\" size=\"4\" style=\"height:60px;font-size:24px;\">&nbsp;&nbsp;<input name=\"btn\" type=\"button\" value=\"跳转\" onclick=\"goPage()\"  style=\"width:90px;height:70px;font-size:24px;font-weight:bold;\"/>"
		 * ); html .append("&nbsp;&nbsp;") .append(
		 * "<span style=\"font-size:"+w+
		 * "px; font-weight:bold\"><span id=\"pageNum\">1</span>/");
		 * html.append(picNum).append("页</span>"); } html.append("</td></tr>");
		 * html.append("<tr id=\"loading\" style=\"text-align:center;\">");
		 * html.append("<td width=\"480\"><img src=\"");
		 * html.append(config.getNetworkAddress()).append(
		 * "/loading.gif\" /><br/> 文件加载中...</td>"); html.append("</tr>"); html
		 * .append(
		 * "<tr id=\"loadError\" style=\"display:none;text-align:center\"><td width=\"100%\">图片加载出误，请与管理员联系!</td></tr>"
		 * ); html .append(
		 * "<tr id=\"imageHtml\" style=\"display:none;text-align:center\"><td>"
		 * );
		 * html.append("<input type=\"hidden\" id=\"fileName\"  value=\"").append
		 * ( fileName).append("\"/>");
		 * html.append("<input type=\"hidden\" id=\"currentPage\"  value=\"0\">"
		 * );
		 * html.append("<input type=\"hidden\" id=\"fileSize\"  value=\"").append
		 * ( fileSize).append("\"/>");
		 * html.append("<input type=\"hidden\" id=\"picNum\"  value=\"").append(
		 * picNum).append("\"/>"); html.append("<img src=\"");
		 * html.append(filePath).append("/0.jpg");
		 * html.append("\" id=\"showImage\" width=\"430\">"); html .append(
		 * "</td></tr><tr><td><input name=\"hisBtn\" type=\"button\" value=\"返回\" onclick=\"javascript:history.go(-1);\" style=\"width:"
		 * +(w*3)+"px;height:"+h+"px;font-size:"+w+"px;font-weight:bold;\"/>");
		 * if (picNum > 1) { html .append(
		 * "&nbsp;&nbsp;<input type=\"button\" value=\"上一页\" onclick=\"prePage()\" style=\"width:"
		 * +(w*4)+"px;height:"+h+"px;font-size:"+w+
		 * "px;font-weight:bold;\"/>&nbsp;&nbsp;<input type=\"button\" onclick=\"nextPage()\" value=\"下一页\" style=\"width:"
		 * +(w*4)+"px;height:"+h+"px;font-size:"+w+"px;font-weight:bold;\"/>");
		 * // html.append(
		 * "&nbsp;&nbsp;<input type=\"text\" id=\"pageNum1\"  value=\"1\" size=\"4\" onchange=\"changePage()\" style=\"height:60px;font-size:24px;\">"
		 * ); // html.append(
		 * "&nbsp;&nbsp;<input name=\"btn\" type=\"button\" value=\"跳转\" onclick=\"goPage()\" style=\"width:90px;height:70px;font-size:24px;font-weight:bold;\"/>"
		 * ); html .append("&nbsp;&nbsp;") .append(
		 * "<span style=\"font-size:"+w+
		 * "px; font-weight:bold\"><span id=\"pageNum1\">1</span>/");
		 * html.append(picNum).append("页</span>"); }
		 * html.append("</td></tr></table>"); html.append("</body></html>");
		 */
		if (config.remote) {
			return html.toString();
		}
		writeFile(html.toString(), savePath);
		return null;
	}

	// execelHtml生成
	public String createXmlHtml(String savePath, String htmlContent,
			Config config) {
		return createXmlHtml(savePath, htmlContent, config, null);
	}

	public String createXmlHtml(String savePath, String htmlContent,
			Config config, String excel) {
		StringBuilder html = new StringBuilder();
		html.append(s).append("</script></head><body>");
		html.append(htmlContent);
		html.append("</body></html>");
		String ss = html.toString();
		if ("excel".equals(excel)) {
			ss = ss == null ? "" : ss.replaceAll("../img", "../../img");
		}
		if (config.remote) {
			return ss;
		}
		writeFile(ss, savePath);
		return null;
	}

	// 取得文件大小
	public long getFileSize(String filePath) {
		File file = new File(filePath);
		if (file.exists()) {
			return file.length();
		}
		return 0;
	}

	// 判断文件是否存在
	public boolean fileIsExit(String filePath) {
		File file = new File(filePath);
		if (file.exists()) {
			return true;
		}
		return false;
	}

	// 获取文件扩展名
	public String getExtension(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int i = filename.lastIndexOf('.');
			if ((i > -1) && (i < (filename.length() - 1))) {
				return filename.substring(i + 1).toLowerCase();
			}
		}
		return null;
	}

	/** 将字符编码转换成UTF-8 */
	public String toUTF_8(String str) throws UnsupportedEncodingException {
		return this.changeCharset(str, UTF_8);
	}

	/** HTML生成图片 */
	public void htmlToImage(String html, String savePath) {
		htmlToImage = new HtmlImageGenerator();
		htmlToImage.loadHtml(html);
		htmlToImage.saveAsImage(new File(savePath));
	}

	// 生成Html文件名
	public String createSaveName() {
		StringBuilder fileName = new StringBuilder();
		fileName.append(String.valueOf(((new java.text.SimpleDateFormat(
				"yyyyMMddhhmmss")).format(new Date()))));
		// fileName.append(new Date().getTime());
		fileName.append(createRandom(8));
		return String.valueOf(fileName);
	}

	// 产生随机数
	public String createRandom(int len) {
		Random random = new Random(new Date().getTime());
		StringBuilder rBuilder = new StringBuilder();
		for (int i = 0; i < len; i++) {
			rBuilder.append(random.nextInt(10));
		}
		return String.valueOf(rBuilder);
	}

	// 删除文件夹

	public void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			java.io.File myFilePath = new java.io.File(filePath);
			myFilePath.delete(); // 删除空文件夹
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// param path 文件夹完整绝对路径
	public boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
				flag = true;
			}
		}
		return flag;
	}

	/**
	 * 写文件
	 * */
	public void writeFile(String content, String savePath) {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try {
			File file = new File(savePath);
			if (file.exists()) {
				file.mkdirs();
			}
			fos = new FileOutputStream(file);
			osw = new OutputStreamWriter(fos, "UTF-8");
			osw.write(content);
			osw.flush();
		} catch (Exception e) {
			log.error("Java流写文件出错(com/xt/attachment/OfficeOther.java)", e);
		} finally {
			try {
				if (fos != null) {
					fos.close();
				}
				if (osw != null) {
					osw.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * 字符串编码转换的实现方法
	 * 
	 * @param str
	 *            待转换的字符串
	 * @param newCharset
	 *            目标编码
	 */
	public String changeCharset(String str, String newCharset)
			throws UnsupportedEncodingException {
		if (str != null) {
			// 用默认字符编码解码字符串。与系统相关，中文windows默认为GB2312
			byte[] bs = str.getBytes();
			return new String(bs, newCharset); // 用新的字符编码生成字符串
		}
		return null;
	}

	/**
	 * String转换为List
	 * */
	public List<String> stringToList(String str, String split) {
		String[] arrStr = str.split(split);
		List<String> list = Arrays.asList(arrStr);
		return list;
	}

	/**
	 * List转换为String
	 * */
	public String listToString(List<String> listStr) {
		StringBuilder strBuilder = new StringBuilder();
		for (int i = 0; i < listStr.size(); i++) {
			strBuilder.append(",").append(listStr.get(i));
		}
		if (strBuilder.length() > 0) {
			return strBuilder.substring(1);
		}
		return null;
	}

	/**
	 * 读取文件
	 * */
	public String openFile(String filePath) {
		InputStreamReader fileReader = null;
		BufferedReader reader = null;
		StringBuilder fileContent = new StringBuilder();
		try {
			File f = new File(filePath);
			fileReader = new InputStreamReader(new FileInputStream(f), ENCODE);
			reader = new BufferedReader(fileReader);
			String line = "";
			while ((line = reader.readLine()) != null) {
				fileContent.append(line);
			}
			reader.close();

		} catch (Exception e) {
			log.error("java流读取文件出错(com/xt/attachment/OfficeOther.java第465行)", e);
		} finally {
			try {
				fileReader.close();
				reader.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return String.valueOf(fileContent);
	}

	public int threadNum(int row, int maxRecord) {
		if (row % maxRecord > 0) {
			return (int) (row / maxRecord) + 1;
		} else {
			return row / maxRecord;
		}
	}
	/**
	 * 去除特殊字符
	 * */
	public  String StringFilter(String str) throws PatternSyntaxException {
		StringBuilder param = new StringBuilder();
		// 只允许字母和数字
		// String regEx = "[^a-zA-Z0-9]";
		// 清除掉所有特殊字符
		String regex = "([\u4e00-\u9fa5]|[0-9]|[a-z|A-Z]+)";
		Matcher matcher = Pattern.compile(regex).matcher(str);
		int k = 0;
		while (matcher.find()) {
			param.append(matcher.group(0));
		}
		return param.toString().replaceAll(" ", "").trim();
	}

	public String getSavePath() {
		return savePath;
	}

	public synchronized void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getFileOldName() {
		return fileOldName;
	}

	public synchronized void setFileOldName(String fileOldName) {
		this.fileOldName = fileOldName;
	}

	public String getFileSpellName() {
		return fileSpellName;
	}

	public synchronized void setFileSpellName(String filePath) {
		Date nowTime = new Date();
		this.fileSpellName = cn2FirstSpell(getFileName(filePath, 1));
	}

	public long getFileOldSize() {
		return fileOldSize;
	}

	public synchronized void setFileOldSize(long fileOldSize) {
		this.fileOldSize = fileOldSize;
	}

	public synchronized void setFileOldSize(String filePath) {
		this.fileOldSize = getFileSize(filePath);
	}

	public int getPicNum() {
		return picNum;
	}

	public synchronized void setPicNum(int picNum) {
		this.picNum = picNum;
	}

	public String getCreateHtmlPath() {
		return createHtmlPath;
	}

	public synchronized void setCreateHtmlPath(String createHtmlPath) {
		this.createHtmlPath = createHtmlPath;
	}

	public String getFileExtension() {
		return fileExtension;
	}

	public synchronized void setFileExtension(String fileExtension) {
		this.fileExtension = fileExtension;
	}

	private String s = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
			+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
			+ "<head>"
			+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=3.0, user-scalable=yes\" />"
			// +"<meta name='viewport' content='target-densitydpi=device-dpi,width=device-width, initial-scale=1.0, user-scalable=yes' />"
			+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
			+ "<title>内容显示页</title>"
			+ "<style>"
			+ "body {margin:0px auto;padding:0px;text-align:left;font-size:15px;font-weight:bold;color:#666;line-height:150%;background:url(../img/bg.png);}"

			+ ".main {margin:0px auto;text-align:center;width:100%;}"
			+ ".border {width:480px;}"
			+ ".main div {margin:0px auto;width:100%;}"

			/*
			 * +".main {margin:0px auto;text-align:center;width:320px;}"
			 * +".main div {margin:0px auto;width:320px;}"
			 */

			+ ".main div.bnt_bar {height:30px;padding-top:3px; background:#809aa1 url(../img/bg.gif) repeat-x;border-top:1px solid #5f7981;border-bottom:1px solid #5f7981;}"
			+ ".main div.bnt_bar input {float:left;margin:0px 0px 0px 5px;width:75px;height:27px;border:none;background:transparent url(../img/bnt1.gif) no-repeat;font-size:20px;font-weight:bold;color:#fff;}"
			+ ".main div.bnt_bar input.over {background:url(../img/bnt2.gif) no-repeat;}"
			+ ".main div.bnt_bar div {float:right;width:65px;line-height:27px;vertical-align:middle;text-align:center;color:#fff;}"
			+ ".main div.load {width:320px;margin:0px auto;line-height:60px;vertical-align:middle;text-align:center;}"
			+ ".main div.showIMG {min-height:320px;margin:2px auto;}"
			+ ".main div.showIMG img{width:100%;height:auto;border:1px solid #ccc;}"
			+ "</style>"
			+ "<script>"
			+ "var curEL = null;"
			+ "function setBnt(el){"
			+ "	if(curEL != el){"
			+ "		if(curEL!=null){"
			+ "			curEL.className = \"\";"
			+ "		}"
			+ "		el.className = \"over\";" + "		curEL = el;" + "	}" + "}";

	/*
	 * +"</script>" +"</head>" +"<body>";
	 */

	/**
	 * json转为map
	 * */
	public Map<String, Object> jsonToMap(String json) {
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject jsonObject = JSONObject.fromObject(json);
		for (Iterator iter = jsonObject.keys(); iter.hasNext();) {
			String key = (String) iter.next();
			map.put(key, jsonObject.get(key));
		}
		return map;
	}
	
	
	public static void main(String[] strs) throws Exception {
		OfficeOther other = new OfficeOther();
		// List<String> list = new ArrayList<String>();
		// list.add("1111");
		// list.add("2222");
		// list.add("3333");
		// list.add("4444");
		// String k = other.listToString(list);
		// System.out.println(other.stringToList(k, ","));
//		System.out
//				.println(other
//						.StringFilter("chuxiongkuangyeyidongoav02_build20130425-bugliebiaoxlsx_1367052974000120.xlsx"));
		String regex="([\u4e00-\u9fa5]|[0-9]|[a-z|A-Z]+)";
		String str="guanyu《shimenhekanghandajingjiejuezhoubiancunminyinshuiguanwanggongcheng》xubuchongshigongtongzhideqingshipdf_1364352334000100";
		Matcher matcher = Pattern.compile(regex).matcher(str);
			int k=0;
			while(matcher.find())
	        {
	              System.out.println(matcher.group(0)); //打印所有
	              k++;
	        }  
	}

}
